package com.fling.hero.state;

import com.badlogic.gdx.math.Vector2;
import com.fling.Locator;
import com.fling.entity.CollisionEntity;
import com.fling.objects.Hero;
import com.fling.observer.Event;

public class SecondJumpState extends HeroState {

	private INPUT jump;
	private boolean holdClimb;
	private Vector2 vel;

	public SecondJumpState(Hero h, INPUT jump) {
		super(h);
		this.jump = jump;
		type=STATE_TYPE.SECOND_JUMP_STATE;
		determineJump();
	}

	public void determineJump() {
		switch (jump) {
		case JUMP_LEFT:
			break;
		case JUMP_DOWN:
			vel.set(0, -hero.Y_JUMP_VEL);
			hero.setSprite(hero.getUpSprite());
			hero.setFlipY(true);
			break;
		case JUMP_RIGHT:
			break;
		case JUMP_UP:
			vel.set(0, hero.Y_JUMP_VEL);
			hero.setSprite(hero.getUpSprite());
			hero.setFlipY(false);
			break;
		default:
			break;
		}
		
	}

	@Override
	public void update(float delta) {
		if(hero.getPosition().y >= SCREEN_HEIGHT - HERO_HEIGHT * Locator.getySpriteScale() - BORDER_HEIGHT){
			vel.set(0, 0);
		}
		tmp.set(vel);
		hero.getBody().setLinearVelocity(tmp.scl(Locator.getXGameSpeed() * Locator.getOverallGameSpeed() * delta, Locator.getYGameSpeed() * Locator.getOverallGameSpeed() * delta));
	}

	@Override
	public void newAction(INPUT input) {
		switch (input) {
		case CLIMB:
			holdClimb=true;
			break;
		case FALL:
			holdClimb=false;
			break;
		case JUMP_RIGHT:
			hero.changeState(new ThirdJumpState(hero, input));
			hero.setFlipY(false);
			break;
		case JUMP_LEFT:
			hero.changeState(new ThirdJumpState(hero, input));
			hero.setFlipY(false);
			break;
		default:
			// hero is jumping
			break;
		}
	}

	@Override
	public void enter() {
		holdClimb=false;
		vel=new Vector2();
		hero.getSubject().notify(hero, Event.EVENT_SECOND_JUMP);
	}

	@Override
	public void exit() {
		if(holdClimb){
			hero.changeState(new ClimbingState(hero));
		} else {
			hero.changeState(new FallingState(hero));
		}
	}

	@Override
	public void collide(CollisionEntity otherBody) {
		if(otherBody.getType().equals(TYPE.WALL) && hero.isExists()){
			exit();
		} else if(otherBody.getType().equals(TYPE.BLOCK)){
			hero.die();
		}
		
	}

}
